package com.zone.mybatis.session.defaults;

import com.zone.mybatis.executor.Executor;
import com.zone.mybatis.mapping.Environment;
import com.zone.mybatis.session.Configuration;
import com.zone.mybatis.session.SqlSession;
import com.zone.mybatis.session.SqlSessionFactory;
import com.zone.mybatis.session.TransactionIsolationLevel;
import com.zone.mybatis.transaction.Transaction;
import com.zone.mybatis.transaction.TransactionFactory;

import javax.sql.DataSource;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2023/12/17
 */
public class DefaultSqlSessionFactory implements SqlSessionFactory {
    private final Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override

    public SqlSession openSession() {
        Environment environment = configuration.getEnvironment();
        TransactionFactory transactionFactory = environment.getTransactionFactory();
        Transaction transaction = transactionFactory.newTransaction(environment.getDataSource(), TransactionIsolationLevel.READ_COMMITTED, false);
        Executor executor = configuration.getExecutor(transaction);

        return new DefaultSqlSession(configuration, executor);
    }
}
